package com.vehicle.ucenter.provider.serviceimpl;

import com.vehicle.ucenter.api.entity.SysMenu;
import com.vehicle.ucenter.api.entity.SysUser;
import com.vehicle.ucenter.api.service.SysUserService;
import com.vehicle.ucenter.provider.mapper.SysMenuMapper;
import com.vehicle.ucenter.provider.mapper.SysRoleMapper;
import com.vehicle.ucenter.provider.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by yantingbin on 2017/7/14.
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public SysUser login(String userName) {
        SysUser user = sysUserMapper.getByUserNameAndPwd(userName);
        if (user == null) {
            return null;
        }
        String userId = user.getId();
        user.setRoles(sysRoleMapper.findListByUserId(userId));

        List<SysMenu> menus = null;

        if (SysUser.ADMIN_USER_ID.equals(userId)) {
            menus = sysMenuMapper.findAllList();
        } else {
            menus = sysMenuMapper.findListByUserId(userId);
        }
        user.setMenus(menus);
        return user;
    }

    @Override
    public SysUser getByUserEmail(String email) {
        return sysUserMapper.getByUserEmail(email);
    }

    @Override
    public int updateInfo(SysUser user) {
        return sysUserMapper.updateInfo(user);
    }
}
